﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections;
using System.Text;
using System.IO;

using FL.DBModule;
using FL.DBModule.Sys;
using FL.SiteEntity;
using FL.Common;


namespace FL.Bussiness.Site
{
    public class BArticle : BBaseSite
    {
        public static BArticle CreateInstance()
        {
            return new BArticle();
        }


        /// <summary>
        /// 获取图片文章列表
        /// </summary>
        /// <param name="category_id"></param>
        /// <returns></returns>
        public IList<v_t_site_article> GetImagesArticleList(string category_no)
        {
            IList<v_t_site_article> list = this.GetEntitys<v_t_site_article>(string.Format("category_code='{0}' and isnull(img_url,'0')<>'0'", category_no));
            return list.OrderBy(p => p.sort).OrderBy(p => p.sort).ThenByDescending(p => p.sort).ToList();
        }


        /// <summary>
        /// 获取图片文章列表
        /// </summary>
        /// <param name="category_no">分类</param>
        /// <param name="pageszie">记录数</param>
        /// <returns></returns>
        public IList<v_t_site_article> GetImagesArticleList(string category_no, string orderField = "", int page_size = 4, int page_index = 1)
        {
            this.PageIndex = page_index;
            this.PageSize = page_size;
            this.Order = orderField;
            IList<v_t_site_article> list = this.GetEntitys<v_t_site_article>(string.Format("category_code='{0}' and isnull(img_url,'')<>''", category_no));
            return list.OrderBy(p => p.sort).OrderBy(p => p.sort).ThenByDescending(p => p.sort).ToList();
        }


        /// <summary>
        /// 文章列表
        /// </summary>
        /// <param name="category_no">分类</param>
        /// <returns>v_t_site_article</returns>
        public IList<v_t_site_article> GetNormalArticleList(string category_no)
        {
            IList<v_t_site_article> list = this.GetEntitys<v_t_site_article>(string.Format("category_code='{0}'", category_no)); ;
            return list.OrderBy(p => p.sort).OrderBy(p => p.add_time).ThenByDescending(p => p.add_time).ToList();
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="category_code"></param>
        /// <returns></returns>
        public IList<t_site_article> GetNormalArticleList(string category_code, string orderField = "", int page_size = 20, int page_index = 1)
        {
            this.PageIndex = page_index;
            this.PageSize = page_size;
            this.Order = orderField;

            t_site_article_category category = this.GetEntityBySqlComm<t_site_article_category>(string.Format("select * from  t_site_article_category where code='{0}'", category_code));
            Guid category_id = Guid.Empty;
            if (category != null)
            {
                category_id = category.id;
            }
            IList<t_site_article> list = this.GetEntitysSplitPage<t_site_article>(string.Format("category_id='{0}'", category_id));
            return list;//.OrderBy(p => p.sort).OrderBy(p => p.sort).ThenByDescending(p => p.sort).ToList();
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="category_code"></param>
        /// <returns></returns>
        public IList<v_t_site_article> GetVArticleList(string category_code, string orderField = "", int page_size = 20, int page_index = 1)
        {
            this.PageIndex = page_index;
            this.PageSize = page_size;
            this.Order = orderField;


            IList<v_t_site_article> list = this.GetEntitysSplitPage<v_t_site_article>(string.Format("category_code='{0}'", category_code));
            return list;//.OrderBy(p => p.sort).OrderBy(p => p.sort).ThenByDescending(p => p.sort).ToList();
        }


        /// <summary>
        /// 获取内容详细信息
        /// </summary>
        /// <param name="article_id">文章ID</param>
        /// <returns></returns>
        public t_site_article GetArticle(Guid article_id)
        {
            return this.GetEntityById<t_site_article>(article_id);
        }


        /// <summary>
        /// 获取广告信息
        /// </summary>
        /// <param name="place_holder"></param>
        /// <returns></returns>
        public t_site_ad GetAd(string place_holder)
        {
            t_site_ad ad = this.GetEntityByCondition<t_site_ad>(string.Format("placeholder='{0}'", place_holder));
            if (ad != null)
            {
                ad.iamges = this.GetEntitys<t_site_ad_image>(string.Format("ad_id='{0}'", ad.id));
            }
            return ad;
        }

        #region 正文导航/分类
        /// <summary>
        /// 正文的导航
        /// </summary>
        /// <param name="article_id">文章的ID</param>
        /// <returns></returns>
        public string GetNavString(Guid article_id)
        {
            t_site_article article = this.GetEntityById<t_site_article>(article_id);
            if(article==null)
            { return ""; }
            //最低一层分类
            t_site_article_category category = this.GetEntityById<t_site_article_category>(article.category_id);
            return GetNavString(category);
            //article.category_id
        }

        
        /// <summary>
        /// 文章的正文导航
        /// </summary>
        /// <param name="category">分类</param>
        /// <returns></returns>
        public string GetNavString(t_site_article_category category)
        {
            if(category ==null||category.name=="root")
            {
                return "";
            }

            string _navString = "";
            _navString = string.Format("<a href=\"/article?id={0}\" target=\"_blank\">{1}</a>",category.id,category.name);
            
            t_site_article_category _parentCategory = this.GetEntityById<t_site_article_category>(category.parent_id);
            return GetNavString(_parentCategory) + ">>" + _navString;
        }

        #endregion

        /// <summary>
        /// 获取模版
        /// </summary>
        /// <param name="code">模版编码</param>
        /// <returns>模版对象</returns>
        public t_site_template GetTemplate(string code)
        {
            t_site_template template = this.GetEntityByCondition<t_site_template>(string.Format("code='{0}'", code));
            return template;
        }

        #region 标签
        /// <summary>
        /// 通过标签找关联信息的文章
        /// </summary>
        /// <param name="tags">标签</param>
        /// <returns></returns>
        public IList<t_site_article> GetUnionArticles(string tags,int pagesize=10,string orderField="add_time",int pageindex=1)
        {
            IList<t_site_article> list = new List<t_site_article>();
            string[] _tags = tags.Split(',');
            StringBuilder strWhere = new StringBuilder("");
            foreach (string tag in _tags)
            {
                if (strWhere.Length > 0)
                {
                    strWhere.Append(" or ");
                }
                strWhere.Append(string.Format("tags like '%{0}%'", tag));
            }
            if (strWhere.ToString() == "")
            {
                return null;
            }
            this.PageIndex = pageindex;
            this.PageSize = pagesize;
            this.Order = orderField;
            this.Desc = "Yes";
            return this.GetEntitysSplitPage<t_site_article>(strWhere.ToString());
        }

        /// <summary>
        /// 通过标签找关联信息的文章
        /// </summary>
        /// <param name="tags">标签</param>
        /// <returns></returns>
        public IList<t_site_article> GetUnionArticles(string tags)
        {
            return this.GetUnionArticles(tags,10,"add_time",1);
        }



        /// <summary>
        /// 通过标签找关联信息的文章
        /// </summary>
        /// <param name="tags">标签</param>
        /// <returns></returns>
        public IList<t_site_article> GetTagArticles(string tag)
        {
            StringBuilder strWhere = new StringBuilder("");
            strWhere.Append(string.Format("tags like '%{0}%'", tag));

            if (strWhere.ToString() == "")
            {
                return null;
            }
            return this.GetEntitys<t_site_article>(strWhere.ToString());
        }

        #endregion


        /// <summary>
        /// 记录对应的查看记录
        /// </summary>
        /// <param name="article_id"></param>
        public void InsertArticleView(Guid article_id, string user_name = "", string ip = "")
        {
            t_site_article_view article_view = new t_site_article_view();
            article_view.article_id = article_id;
            article_view.user_name = user_name;
            article_view.id = Guid.NewGuid();
            article_view.ip = ip;
            this.entity = article_view;
            this.Insert();
            this.RunSqlCommand(string.Format("update t_site_article set read_count=isnull(read_count,0)+1 where id='{0}'", article_id));
        }

    }


}
